home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Online / SANA2Meter / Source / support.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-04  |  9.0 KB  |  401 lines

  1. /*
  2. ** $VER: support.c 1.2 (04 Jun 1996)
  3. **
  4. ** (C) Copyright 1996 Marius Gröger
  5. **     All Rights Reserved
  6. **
  7. ** $HISTORY:
  8. **
  9. ** 04 Jun 1996 : 001.002 :  ToolType handling improved
  10. ** 05 May 1996 : 001.001 :  fixed bug in hex number conversion
  11. ** 25 Apr 1996 : 001.000 :  created
  12. */
  13.  
  14. /*F*/ /* includes */
  15.  
  16. #include <clib/alib_protos.h>
  17. #include <clib/dos_protos.h>
  18. #include <pragmas/dos_pragmas.h>
  19. #include <clib/utility_protos.h>
  20. #include <pragmas/utility_pragmas.h>
  21. #include <clib/exec_protos.h>
  22. #include <pragmas/exec_sysbase_pragmas.h>
  23. #include <clib/intuition_protos.h>
  24. #include <pragmas/intuition_pragmas.h>
  25. #include <clib/gadtools_protos.h>
  26. #include <pragmas/gadtools_pragmas.h>
  27. #include <clib/icon_protos.h>
  28. #include <pragmas/icon_pragmas.h>
  29. #include <clib/locale_protos.h>
  30. #include <pragmas/locale_pragmas.h>
  31.  
  32. #include <gtlayout/gtlayout.h>
  33.  
  34. #include <exec/libraries.h>
  35. #include <exec/devices.h>
  36. #include <exec/memory.h>
  37. #include <exec/io.h>
  38.  
  39. #include <workbench/startup.h>
  40.  
  41. #include <utility/tagitem.h>
  42.  
  43. #include <netinclude:devices/sana2.h>
  44.  
  45. #define AsmPools
  46. #include <pools/pool_lib.h>
  47.  
  48. #if (USE_EXTREADARGS != 0)
  49. #include <extrdargs.h>
  50. #endif
  51.  
  52. #include "compiler.h"
  53. #include "debug.h"
  54. #define CATCOMP_NUMBERS
  55. #include "locale.h"
  56. #include "sana2meter.h"
  57. #include "constdata.h"
  58.  
  59. #include <strings.h>
  60. /*E*/
  61.  
  62. /*F*/ /* private symbols */
  63. PRIVATE int ToolMatch(char *s1, char *s2);
  64. /*E*/
  65. /*F*/ /* exported symbols */
  66. PUBLIC VOID *allocpvec(GD gd, ULONG len);
  67. PUBLIC VOID freepvec(GD gd, VOID *mem);
  68. PUBLIC VOID *allocppvec(GD gd, ULONG len);
  69. PUBLIC VOID freeppvec(GD gd, VOID *mem);
  70. PUBLIC VOID message(GD gd, LONG, LONG, LONG, ...);
  71. PUBLIC struct Library *openlib(GD gd, STRPTR name, LONG version);
  72. PUBLIC STRPTR AddToolType(GD gd, struct DiskObject *dobj, char *tool);
  73. PUBLIC STRPTR *DupToolTypes(GD gd, struct DiskObject *dobj);
  74. PUBLIC void UnDupToolTypes(GD gd, struct DiskObject *dobj, STRPTR *oldtt);
  75. PUBLIC ULONG strntolong(GD gd, STRPTR s, LONG *number, LONG n);
  76. /*E*/
  77. /*F*/ /* imported symbols */
  78. IMPORT ASM STRPTR GetString(REG(a0) struct LocaleInfo *li, REG(d0) LONG stringNum);
  79. /*E*/
  80.  
  81. /*F*/ PUBLIC ULONG strntolong(GD gd, STRPTR s, LONG *number, LONG n)
  82. {
  83.  
  84. #define IsDigit(c) ((c) >= '0' && (c) <= '9'))
  85. #define IsXDigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'F'))
  86.  
  87.    BOOL hex;
  88.    UBYTE c;
  89.    UWORD countdown;
  90.    ULONG preconv, rc;
  91.  
  92.    if ((n > 2) && !Strnicmp(s, "0x", 2))
  93.    {
  94.       hex = TRUE;
  95.       s += 2;
  96.       n -= 2;
  97.       preconv = 2;
  98.    }
  99.    else if ((n > 1) && !Strnicmp(s, "$", 1))
  100.    {
  101.       hex = TRUE;
  102.       ++s;
  103.       --n;
  104.       preconv = 1;
  105.    }
  106.    else
  107.    {
  108.       hex = FALSE;
  109.       preconv = 0;
  110.    }
  111.  
  112.    if (countdown=n)
  113.    {
  114.       LONG num;
  115.  
  116.       for(num=0; countdown && (*s != '\0'); ++s, --countdown)
  117.       {
  118.          c = ToUpper((ULONG)*s);
  119.          if (hex)
  120.          {
  121.             if (c >= '0' && c <= '9') num = num * 16 + c - '0';
  122.             else if (c >= 'A' && c <= 'F') num = num * 16 + c - 'A' + 10;
  123.             else break;
  124.          }
  125.          else
  126.          {
  127.             if (c >= '0' && c <= '9') num = num * 10 + c - '0';
  128.             else break;
  129.          }
  130.       }
  131.  
  132.       if (n - countdown)
  133.       {
  134.          *number = num;
  135.          rc = preconv + (n - countdown);
  136.       }
  137.       else rc = 0;
  138.    }
  139.    else rc = 0;
  140.  
  141.    return rc;
  142. }
  143. /*E*/
  144.  
  145. /*F*/ PUBLIC VOID message(GD gd, LONG title, LONG body, LONG button, ...)
  146. {
  147.    struct EasyStruct es;
  148.  
  149.    es.es_StructSize = sizeof(struct EasyStruct);
  150.    es.es_Flags = 0;
  151.    es.es_Title = GetString(&gd->gd_LocaleInfo, title);
  152.    es.es_TextFormat = GetString(&gd->gd_LocaleInfo, body);
  153.    es.es_GadgetFormat = GetString(&gd->gd_LocaleInfo, button);
  154.  
  155.    if ((gd->gd_Handle != NULL) && (gd->gd_Handle->Window != NULL))
  156.    {
  157.       LT_LockWindow(gd->gd_Handle->Window);
  158.       EasyRequestArgs(gd->gd_Handle->Window, (struct EasyStruct *)&es, NULL, (APTR)(&button + 1));
  159.       LT_UnlockWindow(gd->gd_Handle->Window);
  160.    }
  161.    else
  162.    {
  163.       if (gd->gd_CLI || !IntuitionBase)
  164.       {
  165.          if (gd->gd_DOSBase) VPrintf(es.es_TextFormat, (APTR)(&button + 1));
  166.          VPrintf("\n", NULL);
  167.       }
  168.       else
  169.          EasyRequestArgs(NULL, (struct EasyStruct *)&es, NULL, (APTR)(&button + 1));
  170.    }
  171. }
  172. /*E*/
  173.  
  174. /*F*/ PUBLIC struct Library *openlib(GD gd, STRPTR name, LONG version)
  175. {
  176.    struct Library *lib;
  177.  
  178.    if (!(lib = OpenLibrary(name, version)))
  179.       message(gd, REQ_ERROR_TITLE, REQ_ERROR_OPENLIB, REQ_ERROR_QUITBUTTON,
  180.                                     name, version);
  181.    return lib;
  182. }
  183. /*E*/
  184.  
  185. /*F*/ PUBLIC VOID *allocpvec(GD gd, ULONG len)
  186. {
  187.    ULONG *p;
  188.  
  189.    if (p = AsmAllocPooled(gd->gd_AnyPool, len+sizeof(ULONG), (struct ExecBase*)SysBase))
  190.       *p++ = (ULONG)(len+sizeof(ULONG));
  191.  
  192.    return (VOID*)p;
  193. }
  194. /*E*/
  195. /*F*/PUBLIC VOID freepvec(GD gd, VOID *mem)
  196. {
  197.    ULONG *p = (ULONG*)mem - 1;
  198.  
  199.    AsmFreePooled(gd->gd_AnyPool, p, *p, (struct ExecBase*)SysBase);
  200.  
  201.    return ;
  202. }
  203. /*E*/
  204. /*F*/ PUBLIC VOID *allocppvec(GD gd, ULONG len)
  205. {
  206.    ULONG *p;
  207.  
  208.    if (p = AsmAllocPooled(gd->gd_PubPool, len+sizeof(ULONG), (struct ExecBase*)SysBase))
  209.       *p++ = (ULONG)(len+sizeof(ULONG));
  210.  
  211.    return (VOID*)p;
  212. }
  213. /*E*/
  214. /*F*/PUBLIC VOID freeppvec(GD gd, VOID *mem)
  215. {
  216.    ULONG *p = (ULONG*)mem - 1;
  217.  
  218.    AsmFreePooled(gd->gd_PubPool, p, *p, (struct ExecBase*)SysBase);
  219.  
  220.    return ;
  221. }
  222. /*E*/
  223.  
  224.  
  225.  
  226. /* ------------------------------------------------------------------------- */
  227. /* This code has been grabbed by Jure Vrhovnik's supra.lib library and was   */
  228. /* modified that it doesn't use the Undocumented FreeList handling of        */
  229. /* icon.library. This should make the code much more safe.                   */
  230. /*F*/ PRIVATE int ToolMatch(char *s1, char *s2)
  231. {
  232. int cmp = 2;
  233. int offs = 0;
  234.  
  235.     if ((*s1 == '(' || *s2 == '(') && *s1 != *s2) offs = 3;
  236.     if (*s1 == '(') s1++;
  237.     if (*s2 == '(') s2++;
  238.     if (*s1 == ')') {
  239.             if (*(s1+1) == '\0') s1++;
  240.     }
  241.  
  242.     if (*s2 == ')') {
  243.         if (*(s2+1) == '\0') s2++;
  244.     }
  245.  
  246.  
  247.     while (*s1 && *s2) {
  248.         if (*s1 == '\0' && *s2 == '\0') return(0+offs);
  249.         if (*s1 != *s2) return(cmp+offs);
  250.         if (*s1 == '=') cmp=1;
  251.         s1++; s2++;
  252.         if (*s1 == ')') {
  253.             if (*(s1+1) == '\0') s1++;
  254.         }
  255.  
  256.         if (*s2 == ')') {
  257.             if (*(s2+1) == '\0') s2++;
  258.         }
  259.  
  260.     }
  261.  
  262.     if (*s1 == '\0' && *s2 == '\0') return(0+offs);
  263.     else return(cmp+offs);
  264. }
  265. /*E*/
  266. /*F*/ PRIVATE void FreeToolTypes(GD gd, struct DiskObject *dobj, STRPTR *tooltypes)
  267. {
  268.    int i;
  269.  
  270.    if (tooltypes != NULL)
  271.    {
  272.       for(i=0; tooltypes[i] != NULL; ++i)
  273.          freeppvec(gd, tooltypes[i]);
  274.  
  275.       freeppvec(gd, tooltypes);
  276.  
  277.       dobj->do_ToolTypes = tooltypes;
  278.    }
  279.    return;
  280. }
  281. /*E*/
  282. /*F*/ PUBLIC void UnDupToolTypes(GD gd, struct DiskObject *dobj, STRPTR *oldtt)
  283. {
  284.    FreeToolTypes(gd, dobj, dobj->do_ToolTypes);
  285.    dobj->do_ToolTypes = oldtt;
  286.    return;
  287. }
  288. /*E*/
  289. /*F*/ PUBLIC STRPTR *DupToolTypes(GD gd, struct DiskObject *dobj)
  290. {
  291.    STRPTR *orga = dobj->do_ToolTypes;
  292.    STRPTR *newa, p;
  293.    STRPTR *rv = orga;
  294.    int i,n;
  295.  
  296.    for(n=0; orga[n] != NULL; ++n)
  297.       ;
  298.  
  299.    if (newa = allocppvec(gd, (n+1) * sizeof(STRPTR)))
  300.    {
  301.       for(i=0; i<n; ++i)
  302.       {
  303.          if (p = allocppvec(gd, strlen(orga[i]) + 1))
  304.          {
  305.             strcpy((char*)p, (char*)orga[i]);
  306.             newa[i] = p;
  307.          }
  308.          else
  309.          {
  310.             while(i--) freeppvec(gd, newa[i]);
  311.             freeppvec(gd, newa);
  312.             rv = NULL;
  313.             break;
  314.          }
  315.       }
  316.       if (rv)
  317.       {
  318.          orga[i] = NULL;
  319.          dobj->do_ToolTypes = newa;
  320.       }
  321.    }
  322.  
  323.    return rv;
  324. }
  325. /*E*/
  326. /*F*/ PUBLIC STRPTR AddToolType(GD gd, struct DiskObject *dobj, char *tool)
  327. {
  328.    STRPTR **ttypes = &(dobj->do_ToolTypes);
  329.    STRPTR item = (*ttypes)[0];
  330.    int change = -1;
  331.    int i=0;
  332.    STRPTR newmem;
  333.    int len;
  334.    STRPTR rv = NULL;
  335.  
  336.    while (item != NULL)
  337.    {
  338.       if (change == -1)  /* We haven't found any matching tooltype yet */
  339.       {
  340.          switch(ToolMatch(item, tool))
  341.          {
  342.             case 0:
  343.                rv = item;     /* This tooltype already defined */
  344.             goto leave;
  345.  
  346.             case 1:
  347.             case 3:
  348.             case 4:
  349.                change = i;     /* Change this tooltype */
  350.             break;
  351.  
  352.             default:
  353.                /* 2,5 means different */
  354.             break;
  355.          }
  356.       }
  357.  
  358.       i++;
  359.       item = (*ttypes)[i]; /* Get the next tooltype to be examined */
  360.    }
  361.  
  362.    if (change != -1)
  363.    {
  364.       len = strlen(tool)+1;
  365.       if ((newmem = allocppvec(gd, len)) != NULL)
  366.       {
  367.          strcpy(newmem, tool);
  368.          freeppvec(gd, (*ttypes)[change]);
  369.          (*ttypes)[change] = newmem;
  370.          rv = newmem;
  371.       }
  372.    }
  373.    else
  374.    {
  375.       len = (i+2) * sizeof(STRPTR);
  376.       if ((newmem = allocppvec(gd, len)) != NULL)
  377.       {
  378.          CopyMem(*ttypes, newmem + sizeof(STRPTR), len);
  379.  
  380.          freeppvec(gd, *ttypes);
  381.  
  382.          *ttypes = (STRPTR *)newmem;
  383.  
  384.          len = strlen(tool)+1;
  385.          if ((newmem = allocppvec(gd, len)) != NULL)
  386.          {
  387.             strcpy(newmem, tool);
  388.             (*ttypes)[0] = newmem;
  389.             (*ttypes)[i+1] = NULL;
  390.             rv = newmem;
  391.          }
  392.       }
  393.    }
  394.  
  395. leave:
  396.    return rv;
  397. }
  398. /*E*/
  399. /* ------------------------------------------------------------------------- */
  400.  
  401.